﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Simple.Service;
using Simple.Common.Infrastructure;
using Autofac;
using Simple.Common.EasyUICommon;
using Simple.ViewModel;
using Simple.Entity.Enums;

namespace Simple.Web.Framework.Infrastructure.Aop
{
    /// <summary>
    /// 标示为菜单视图
    /// </summary>
    public class MenuViewFilterAttribute : ActionFilterAttribute
    {
        public FormsAuthenticationService _formsAuthenticationService { get; set; }
        /// <summary>
        /// 菜单名称
        /// </summary>
        public string MenuName { get; set; }

        /// <summary>
        /// 模块名称
        /// </summary>
        public string ModularName { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int Sort { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = _formsAuthenticationService.GetAuthenticatedCustomer();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var controllerName = filterContext.RouteData.Values["controller"].ToString();

            var currentMenu = user.MenuList.FirstOrDefault(m => m.ControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && m.ActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase));
            if (currentMenu == null)
            {
                //没有菜单权限
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonNetResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.DenyGet,
                        Data = SimpleResult.GetError("您没有操作权限")
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult() { Content = "您没有操作权限" };
                }
            }
            else
            {
                var Id = currentMenu.Id;
                filterContext.Controller.ViewBag.ButtonList = user.MenuList.Where(m => m.ParentId == Id && m.MenuType == MenuTypeEnum.菜单按钮).ToList();
            }
        }
    }

    /// <summary>
    /// 标示为菜单视图
    /// </summary>
    public class ActionViewFilterAttribute : ActionFilterAttribute
    {
        public FormsAuthenticationService _formsAuthenticationService { get; set; }
        /// <summary>
        /// 动作名称
        /// </summary>
        public string ActionName { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string MenuName { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        /// 事件类型
        /// </summary>
        public ActionTypeEnum ActionType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = _formsAuthenticationService.GetAuthenticatedCustomer();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var controllerName = filterContext.RouteData.Values["controller"].ToString();

            var currentMenu = user.MenuList.FirstOrDefault(m => m.ControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && m.ActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase));
            if (currentMenu == null)
            {
                //没有菜单权限
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonNetResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.DenyGet,
                        Data = SimpleResult.GetError("您没有操作权限")
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult() { Content = "您没有操作权限" };
                }
            }
        }
    }

}